* Test whether the CAP predicts which political party is in power (R1)
* 4/22/2025

clear all
set more off

program main
	ttest_num_policies
	
	clean_state_legislature_data, house(Lower)
	prepare_data
	regressions
end

program ttest_num_policies // formal test for difference in # of SNAP-related policies in treated vs control
	use "$for_analysis/state_for_regressions_actual_ssi", clear
	gen treated = (startyear != . & year >= startyear)
	gen ever_treated = (startyear != .)
	
	* other SNAP policies (from USDA database)
	de has_*, f
	egen num_policies = rowtotal(has_*)
	keep statefip year treated ever_treated num_policies
	duplicates drop
	gisid statefip year
	
	* test whether the number of policies adopted is determined by CAP treatment status
	reghdfe num_policies ever_treated, a(year) vce(cl statefip) // ever treated
	reghdfe num_policies treated, a(year) vce(cl statefip) // treated by CAP that year
end

program clean_state_legislature_data
	syntax, house(str)
	
	* data from https://www.census.gov/library/publications/2011/compendia/statab/131ed/elections.html
	* sheet 419 -- Composition of State Legislatures by Political Party Affiliation
	* downloaded May 31, 2025
	import excel using "$data/raw/12s0419.xls", clear firstrow
	drop if _n < 3  // drop extraneous header rows
	ren Tablewithrow state
	drop if _n > 54   // drop footnotes
	
	ds state, not
	foreach var of varlist `r(varlist)' { // extract true variable names
		replace `var' = `var' + `var'[_n-1] + `var'[_n-2] if _n == 3
	}
	drop if _n < 3
	drop if _n == 2 // aggregate US counts
	
	replace state = trim(substr(state, 1, strpos(state, "\") - 1)) 
	
	* carry the year and house through each variable name
	local curr_suffix = ""
	ds state, not
    foreach var of varlist `r(varlist)' {
        local thisstr = "`= `var' [1]'"
        if substr("`thisstr'", 1, 8) == "Democrat" {
            local curr_suffix = substr("`thisstr'", 9, .)
        }
        else {
            if inlist("`thisstr'", "Republican", "Other", "Vacancies") {
                local party = "`thisstr'"
                local newname = "`party'`curr_suffix'"
                replace `var' = "`newname'" in 1
            }
        }
    }

	* remove unnecessary string and spaces
	ds state, not
	foreach var of varlist `r(varlist)' {
		replace `var' = subinstr(`var', " ", "", .) if _n == 1
		replace `var' = subinstr(`var', "\1", "", .) if _n == 1
	}
	
	* extract true variable names
	ds state, not
	foreach var of varlist `r(varlist)' {
		di subinstr(`var', " ", "_", 1)
	
		local first_value = strtoname(`var'[1]) 
		ren `var'  `first_value' 
	}
	drop if _n == 1
	
	ds *Lowerhouse *Upperhouse
	foreach var of varlist `r(varlist)' { // more variable name cleaning
		local new = subinstr("`var'", "Lowerhouse", "Lower_house", .)
		local new = subinstr("`new'", "Upperhouse", "Upper_house", .)
		ren `var' `new'
	}

	keep state *`house'*  // restrict to state Lower House
	foreach var of varlist *`house'* {  // remove non-numeric characters and destring
		replace `var' = "" if ///
			`var' == "(\6)" | `var' == "(\5)"
		destring `var', replace
	}
	
	foreach year in 1994 1996 1998 2000 2001 ///
		2002 2003 2005 2006 2007 2008 2009 2010 2011 { // generate share Dem in each year
			gen total`year' = Democrat`year'`house'_house + Republican`year'`house'_house ///
				+ Other`year'`house'_house + Vacancies`year'`house'_house
				
			gen shareDem`year' = Democrat`year'`house'_house / total`year'
	}
	keep state share*
	drop if state == "Nebraska" // no data
	
	reshape long shareDem, i(state) j(year)
	save "$for_analysis/state_`house'_partisanship", replace
end

program prepare_data
	use "$intermediate/states_raw_0016", clear
	
	keep statefip *_startyear
	duplicates drop
	gisid statefip
	drop if inlist(statefip, 11, 31) // Washington D.C. and Nebraska (no political data)
	
	* create a panel from 1995-2011
	expand 17
	bys statefip: gen year = _n + 1994
	
	foreach policy in bbce call_any faceini facerec oapp no_fp  { // other state SNAP policies
		gen has_`policy' = (year > `policy'_startyear)
		drop `policy'
	}
	ren cap_startyear startyear
	replace startyear = 1995 if statefip == 45	// South Carolina
	gen adopt_cap = (startyear == year)
	drop startyear
	cap drop reportsimple vehexcl_startyear nonciteld
	
	* merge in state-year policital data
	decode statefip, gen(state)
	replace state = strproper(state)
	
	merge 1:1 state year using "$for_analysis/state_Lower_partisanship", ///
		assert(1 2 3) keep(1 2 3) gen(merge_political)
	assert year == 1994 if merge_political == 2
	drop merge_political
	
	* fill in missing values
	bys state: egen statefip_max = max(statefip)
	replace statefip = statefip_max
	assert !mi(statefip)
	drop statefip_max state

	replace adopt_cap = 0 if mi(adopt_cap)
	sort statefip year
	assert !mi(shareDem) if year == 1994
	replace shareDem = shareDem[_n-1] if mi(shareDem) // replace with prior year if no election
	drop if year == 1994
	
	gen majorityDem = (shareDem > 0.5)
	save "$for_analysis/political_party", replace
end

program regressions
	use "$for_analysis/political_party", clear
	
	* regressions: check whether adopting the CAP predicts which party is in power
	reg majorityDem adopt_cap, vce(cl statefip)
	est sto majority1
	qui estadd local statefe " ", replace
	qui estadd local yearfe " ", replace
	
	reg shareDem adopt_cap, vce(cl statefip)
	est sto share1
	qui estadd local statefe " ", replace
	qui estadd local yearfe " ", replace
	
	* add controls for other state SNAP policies
	reghdfe majorityDem adopt_cap, a(statefip year) vce(cl statefip)
	est sto majority2
	qui estadd local statefe "\checkmark", replace
	qui estadd local yearfe "\checkmark", replace
	
	reghdfe shareDem adopt_cap, a(statefip year) vce(cl statefip)
	est sto share2
	qui estadd local statefe "\checkmark", replace
	qui estadd local yearfe "\checkmark", replace
	
	* generate table
	esttab share1 share2 majority1 majority2, ///
		keep(adopt_cap) order(adopt_cap) ///
		varlabels(adopt_cap "CAP in Place") ///
		se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(statefe yearfe N, ///
		labels("State FE" "Year FE" "N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label nonote booktabs
	
	esttab share1 share2 majority1 majority2 ///
		using "$output/political_party_reg", replace ///
		keep(adopt_cap) order(adopt_cap) ///
		varlabels(adopt_cap "CAP in Place") ///
		se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(statefe yearfe N, ///
		labels("State FE" "Year FE" "N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label nonote booktabs
end


* Execute
main
